package overunder

import (
	"match-spider/util/boltdb"
	"github.com/boltdb/bolt"
	"fmt"
	"strings"
	"github.com/gocolly/colly"
	"time"
	"math/rand"
	"match-spider/util/queue"
)

var failedOverunder=false
var failedTimesOverunder=1

func overunderSpider(dbKey string,bucket string,id string,isRepeat bool)  {
	url:="http://121.10.245.46:8072/phone/OverUnder.aspx?ID=" +id + "&an=iosQiuTan&av=6.5&from=2&lang=0&r="

	c := colly.NewCollector()
	c.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"

	c.OnResponse(func(response *colly.Response) {
		content:=string(response.Body)
		if strings.Compare(strings.TrimSpace(content),"")==0{
			//请求失败
			queue.EnqueueString("./data_schedule/overunder",bucket+"^"+id)
			failedOverunder=true
			if isRepeat{
				failedTimesOverunder++
			}

		}else{
			boltdb.Put(dbKey,bucket,id,content)
		}

	})

	// Before making a request print "Visiting ..."
	c.OnRequest(func(r *colly.Request) {
		fmt.Println("Visiting", r.URL.String())
	})
	c.OnError(func(r *colly.Response, err error) {
		fmt.Println("Error Request URL:", r.Request.URL)
		queue.EnqueueString("./data_schedule/overunder",id)
		failedOverunder=true
		if isRepeat{
			failedTimesOverunder++
		}
	})

	c.Visit(url)
}
/**
大小
 */
func overunderFailProcess(){
	if failedOverunder{
		//休眠
		time.Sleep(time.Minute*time.Duration(3*failedTimesOverunder))
	}
	data:=queue.Dequeue("./data_schedule/overunder")

	if data==nil{
		return
	}
	failedOverunder=false
	s:=strings.Split(string(data.Value),"^")

	if(len(s)<2){
		overunderFailProcess()
		return
	}
	overunderSpider("overunder",s[0],s[1],true)
	seed:=200+rand.Intn(300)
	time.Sleep(time.Millisecond*time.Duration(seed))
	overunderFailProcess()
}
func Fetch()  {

	boltdb.ScanTx("matchs", func(tx *bolt.Tx) {

		tx.ForEach(func(name []byte, b *bolt.Bucket) error {

			b.ForEach(func(k, v []byte) error {
				value:=string(v)
				s:=strings.Split(value,"^")

				asiaOdds:=strings.TrimSpace(s[7])
				underOverOdds:=strings.TrimSpace(s[8])
				if asiaOdds!=""&&underOverOdds!=""{
					//抓取

					league:=strings.TrimSpace(s[1])
					overunderSpider("overunder",league,string(k),false)
					if failedOverunder{
						failedOverunder=false
						time.Sleep(time.Second*90)
					}else{
						seed:=200+rand.Intn(300)
						time.Sleep(time.Millisecond*time.Duration(seed))
					}

				}
				return nil
			})
			return nil
		})

	})

	fmt.Println("================= queue begin =================")
	//处理失败记录
	overunderFailProcess()

}
